$('.addspecial').click(function(){
    $('.special').append($(".setspecial option:selected").clone());
})

var V=new Vue({
    el:'#main',
    data:()=>({
        type:'',
        langs:[],
        pageSize:30,
        dataval:[

        ],
        brand:[],
        brandall:[],
        brandval:0, //品牌的值
        ncm:'',//海关编号
        productCate:[], //产品分类的值
        procate:[],//被选中的产品分类值
        productBrand:[], //品牌
        lang:'',
        cheradioval:1,  //状态
        isfei:1,  //是否可售
        isgeneral:2,
        showcat:'',
        title:'',
        attrarr:{},//待选特征
        seladdsttr:0,
        alllangeclass:{},
        attris_show:true,
        //介绍
        listdata:[],
        page:1,
        sousuo:'',
        order: 1,
        pro_id:0,
        //配件
        itemlistdata:[],
        prochar:[],
        prochar_id:0,
        //图片
        imglist:[],
    }),
    methods:{
        formatUnit:formatUnit,
        getdata(){  //获取概要数据方法
            var data={
                lang:this.lang,
            }
            if($('#type').val()=='edit'){
                var geturl="/admin/product/productEdit?request=edit&id="+$('#did').val()
            }else{
                var geturl="/admin/product/productAdd?request=add"  
            }
            $.ajax({
                url:geturl,
                type:'post',
                data,
                success:(data)=>{
                    data = JSON.parse(data)
                    
                    this.title=data.title   //标题
                    this.alllangeclass=data.product_cate_data  //产品分类
                    this.productCate=data.product_cate_data.zh//
                    this.brand=data.product_brand_data.zh
                    this.brandall=data.product_brand_data  //品牌
                    this.showcat=""
                    this.attrarr=data.profeature   //特征
                    if(!this.attrarr.zh.length){
                        this.attris_show=false
                    }
                    for (const key in this.attrarr) {
                        this.attrarr[key].forEach(element => {
                            element.hasadd=false    
                        });
                    }
                    if($('#type').val()=='edit'){
                        this.procate=data.procate
                        this.procate.forEach(element => {
                            this.productCate.forEach(item => {
                                if(item.id==element){
                                    if(this.showcat.length){
                                        this.showcat+=' / '
                                        this.showcat+=item.content
                                    }else{
                                        this.showcat=item.content
                                    }
                                }
                            });   
                        });
                        data.prochar.zh[0].symbol.forEach(item => {
                            for (const key in this.attrarr) {
                                this.attrarr[key].forEach(element => {
                                    if(element.id==item.id)
                                    element.hasadd=true
                                });
                            }
                        });
                        if(this.attrarr.zh.every(element=>{
                            return element.hasadd
                        })){
                            this.attris_show=false
                        }else{
                            this.attris_show=true
                        }   
                        this.dataval.map((item,index)=>{
                            item.datvis=data.prochar[this.langs[index].code]
                            item.proname=data.proinfo[this.langs[index].code].name
                            item.proint=data.proinfo[this.langs[index].code].prodesc
                            this.brandval=data.proinfo.zh.brand_id
                            this.ncm=data.proinfo.zh.ncm
                        })
                    }
                    $('.loding').hide()
                }
            })
        },
        getlistdata(){
            $.ajax({
                url:'/admin/product/proaddnewjump?request=intro&pro_id='+$('#did').val(),
                data: {
                    page: this.page,
                    limit: 10,
                    lang: this.lang
                },
                type:'post',
                success:(data)=>{                    
                    data = JSON.parse(data)
                    console.log(data);
                    this.title = data.title
                    this.pro_id = data.pro_id
                    this.listdata=data.prodesc_cat
                }
            })
        },
        sortfun($event){  //排序
            this.order = $(event.target).attr('data-order')
            this.listdata.sort(this.sortoneData);
        },
        sortoneData(a, b) {
            if (this.order == 1) {
                $(event.target).attr('data-order', 2)
                return b.update_at - a.update_at
            } else {
                $(event.target).attr('data-order', 1)
                return a.update_at - b.update_at
            }
        },
        requestDel(id,pro_id){
            $.ajax({
                url:'/admin/product/proAddNewJump_introDel',
                data: {
                    prodesc_cat_id:id,
                    pro_id:pro_id
                },
                type:'post',
                success:(data)=>{
                    data = JSON.parse(data)
                    if (data.status != 1) {
                        change_mes_type('.modal-body .alert',data.status)
                        $('.modal-body .alert').show()
                        $('.modal-body .alert p').html('*'+data.msg+'<br>')
                    } else {
                        change_mes_type('.alert',data.status)
                        $('.alert').show()
                        $('.alert p').html('*'+data.msg+'<br>')
                        this.getlistdata()
                    }
                }
            })
        },
        subfn(type){
            var data={
                cateval:this.procate,//产品分类
                brandval:this.brandval,//品牌的值
                statue:this.cheradioval,//状态值
                isfei:this.isfei,//状态值
                isgeneral:this.isgeneral,//状态值
                ncm:this.ncm,//状态值
            }
            this.langs.forEach((item,index)=>{
                data[item.code]=this.dataval[index]
            })
            this.productInsert(data,type)
        },
        productInsert(data,type){
            if($('#type').val()=='edit'){
                var url="/admin/product/productEdit?request=update&id="+$('#did').val()
            }else{
                var url="/admin/product/productAdd?request=insert"  
            }
            $.ajax({
                url:url,
                type:'post',
                data,
                success:(data)=>{
                    data = JSON.parse(data)
                    change_mes_type('.alert',data.status)
                    if(data.status==1){
                        $('.alert').show()
                        $('.alert p').html('*'+data.msg+'<br>')
                        $('#fanhui').attr('noSave','true')
                        if(type=='back'){
                            this.fanhui()
                        }
                        this.getdata()
                    }else{
                        $('.alert').show()
                        $('.alert p').html('*'+data.msg+'<br>')
                        $('#fanhui').attr('noSave','false')
                    }
                }
            }) 

        },
        setTzfun(index){
            if(this.seladdsttr){
                $.ajax({
                    url:'/admin/product/productAdd?request=add&getcontent=feature&id='+this.seladdsttr,
                    type:'post',
                    success:(data)=>{
                        data = JSON.parse(data)
                        /*var newattr=[{key:'',id:'',selval:0,value:[]},{key:'',id:'',selval:0,value:[]},{key:'',id:'',selval:0,value:[]}] */
                        var newattr=[]
                        this.langs.forEach(()=>{
                            newattr.push({key:'',id:'',selval:0,value:[]})
                        })
                        data.data.forEach(element => {
                            this.langs.forEach((item,index)=>{
                                newattr[index].key=element['featureName_'+item.code]
                                newattr[index].id=element.feature_id
                                newattr[index].value.push({name:element['name_'+item.code],val:element.id})
                            })
                        });
                        
                        this.dataval.map((element,index)=>{
                            element.datvis.map(item=>{
                                item.symbol.push(JSON.parse(JSON.stringify(newattr[index])))
                            })
                        })
                        for (const key in this.attrarr) {
                            this.attrarr[key].forEach(element => {
                                if(element.id=this.seladdsttr){
                                    element.hasadd=true
                                }
                            });
                        }
                        if(this.attrarr.zh.every(element=>{
                            return element.hasadd
                        })){
                            this.attris_show=false
                        }
                        this.seladdsttr=0
                    }
                })
            }
        },
        addvis(lang,index){ //添加一行
            var barcode=''
            $.ajax({
                url:'/admin/api/getProcharCodebar',
                type:'post',
                success:(data)=>{
                    barcode=data
                    this.dataval.map(item=>{
                        var data=JSON.parse(JSON.stringify(item.datvis[index]))
                        // data.codebar=barcode
                        if($('#type').val()=='edit'){
                            data.id = 0
                            data.codebar = ''
                        } 
                        item.datvis.push(data)
                    })
                }
            })
        },
        delvis(lang,index){  //删除行
            this.dataval.forEach(element => {
                if(element.datvis.length>1)
                element.datvis.splice(index,1)
            });
        },
        changelange(lang){
            this.lang=lang
        },
        changeradio(val){
            this.cheradioval=val
        },
        changefei(val){
            this.isfei=val
        },
        enter(){
            this.showcat=""
            this.procate=[]
            $('.formbox').serializeArray().forEach(element => {
                element.value=element.value.split('/')
                if(this.procate.indexOf(element.value[0])){
                      if(this.showcat.length){
                            this.showcat+='/'
                            this.showcat+=element.value[1]
                        }else{
                            this.showcat=element.value[1]
                        }
                    this.procate.push(element.value[0])
                }
                      
            });
            $('#procat_setModal').modal('toggle')
        },
        changeselval(row,index,event){
            this.dataval.map(item=>{
                item.datvis[row].symbol[index].selval=event.target.value
            })
        },
        delclass(index){
            var id
            this.dataval.map((item)=>{
                item.datvis.map((item)=>{
                    id=item.symbol[index].id
                    item.symbol.splice(index,1)
                })
            })
            for (const key in this.attrarr) {
                this.attrarr[key].forEach(element => {
                    if(element.id=id)
                    element.hasadd=false
                });
            }
            if(this.attrarr.zh.every(element=>{
                return element.hasadd
            })){
                this.attris_show=false
            }else{
                this.attris_show=true
            }
        },
        fanhui(){
            window.location.href="/admin/product/productlist"
        },
        changeval(index,key,e){
            this.dataval.map((element)=>{
                element.datvis[index][key]=e.target.value
            })
        },
        getBarCode(index,key,e){
            // if($('#type').val()=="add"){
                var newbarcode=''
                $.ajax({
                    url:'/admin/api/getProcharCodebar',
                    type:'post',
                    success:(data)=>{
                        newbarcode=data
                        this.dataval.map((element)=>{
                            element.datvis[index][key]=newbarcode
                        })
                    }
                })
            // }
        },
        getitemdata(){
            this.title="配件"
            this.prochar_id = $("#itemProchar").val()
            $.ajax({
                url:'/admin/product/proAddNewJump?request=item&pro_id='+$('#did').val(),
                data: {
                    page: this.page,
                    limit: this.pageSize,
                    lang: this.lang,
                    prochar_id:this.prochar_id
                },
                type:'post',
                success:(data)=>{
                    data = JSON.parse(data)
                    console.log(data.itemSet);
                    this.pro_id = data.pro_id
                    this.prochar = data.procharlist                  
                    this.itemlistdata=data.itemSet
                    if(data.size)
                    this.setPage(1,Math.ceil(data.size /2))
                }
            })
        },
        
        setPage(pageCurrent, pageSum) {
            //数据总条数
            var myPageCount = pageSum;
            //一页显示多少条
            var myPageSize = this.pageSize;
            //总页数
            var countindex = myPageCount % myPageSize > 0 ? (myPageCount / myPageSize) + 1 : (myPageCount / myPageSize);
            $.jqPaginator('#pagination', {
                totalPages: parseInt(countindex),
                //最多显示多少页
                visiblePages: 5,
                //默认显示第几页
                currentPage: pageCurrent,
                first: '<li class="first"><a href="javascript:;">'+langcon.page_home+'</a></li>',
                prev: '<li class="prev"><a href="javascript:;"><i class="arrow arrow2"></i>'+langcon.previous_page+'</a></li>',
                next: '<li class="next"><a href="javascript:;">'+langcon.next_page+'<i class="arrow arrow3"></i></a></li>',
                last: '<li class="last"><a href="javascript:;">'+langcon.last_page+'</a></li>',
                page: '<li class="page"><a href="javascript:;">{{page}}</a></li>',
                all:'<li class="page"><a href="javascript:;">'+myPageCount+' / '+this.page+'PG , '+this.pageSize+' /PG</a></li>',
                onPageChange:(num, type)=>{
                    if (type == "change") {
                        this.page=num
                        this.getdata()
                    }
                }
            });
        },
        changeprochar(){
            this.prochar_id = $('#prochar option:selected').val()
            this.getdata();
        },
        itemChangeStatus(id){
            $.ajax({
                url:'/admin/product/proAddNewJump_itemDel',
                data: {
                    id:id
                },
                type:'post',
                success:(data)=>{
                    data = JSON.parse(data)
                    if (data.status != 1) {
                        change_mes_type('.modal-body .alert',data.status)
                        $('.modal-body .alert').show()
                        $('.modal-body .alert p').html('*'+data.msg+'<br>')
                    } else {
                        change_mes_type('.alert',data.status)
                        $('.alert').show()
                        $('.alert p').html('*'+data.msg+'<br>')
                        this.getitemdata()
                    }
                }
            })
        },
        getimgdata(){
            this.title="图片"
            $.ajax({
                url:'/admin/product/proaddnewjump?request=img',
                data:{
                    pro_id:$('#did').val()
                },
                type:'post',
                success:(data)=>{
                    data = JSON.parse(data)
                    this.imglist=data.imglist
                }
            })
        },
        delimg(id){
            $.ajax({
                url:'/admin/product/imgDel',
                type:'POST',
                data:{id:id},
                success:(data)=>{
                    data = JSON.parse(data)
                    change_mes_type('.alert',data.status)
                    if(data.status==1){
                    $('.alert').show()
                    $('.alert p').html('*'+data.msg+'<br>')
                    this.getimgdata()
                    }else{
                    $('.alert').show()
                    $('.alert p').html('*'+data.msg+'<br>')
                    }
                }
            });
        }
    },
    mounted(){
        $('.loding').show()
        //console.log($('#type').val());
        this.type=$('#type').val()
        /* 获取语言种数 */
        $.ajax({
            url:'/admin/api/getLangType',
            type:'post',
            //async:false,
            success:data=>{
                this.langs = JSON.parse(data)
                /* 根据语言数量创建数据类型 */
                this.langs.forEach((item,index)=>{
                    this.dataval.push({
                        proname:'',//产品名称
                        proint:'',//简介
                        procalss:[],
                        datvis:[    //行
                            {
                                model:'',//型号
                                factory_model:'',
                                package_qty:1,//包装
                                qty:1,//数量
                                ctn_size:'',//尺寸
                                ctn_kgs:'',//重量
                                ctn_note:'',//说明
                                codebar:'',//条码    
                                symbol:[
                                ]
                            }
                        ]
                    })
                })
            }
        })
        this.getdata()
        $('#procat_setModal').on('show.bs.modal', (e)=> {
        })
        $('#prointro_editModal').on('hidden.bs.modal',(e) =>{
            $("#proTab li").click(function (e) {
                e.preventDefault()
                $(this).tab('show')
                window.location.href = $(this).children('a').attr('href')
            })
            this.getdata()
        });
        $('#prointro_editModal').on('hidden.bs.modal',(e) =>{
            $("#proTab li").click(function (e) {
                e.preventDefault()
                $(this).tab('show')
                window.location.href = $(this).children('a').attr('href')
            })
            this.getlistdata()
        });
        $('#pro2item_editModal').on('hidden.bs.modal',(e)=>{
            this.getitemdata()
        })
        $('#proitem_newModal').on('hidden.bs.modal',(e)=>{
            this.getitemdata()
        })
        $('#proimgs_uploadModal').on('hidden.bs.modal',(e)=>{
            this.getimgdata()
        })
        $('#proimg_editModal').on('hidden.bs.modal',(e)=>{
            this.getimgdata()
        })
    },
    watch:{
        lang:function(){
            this.productCate=this.alllangeclass[this.lang]
            this.brand=this.brandall[this.lang]
        }
    }
})

$("#pro2item_editModal").on("hidden.bs.modal", function () {
    $(this).removeData("bs.modal");
    $(".modal-content").children().remove();
});
$("#proitem_newModal").on("hidden.bs.modal", function () {
    $(this).removeData("bs.modal");
    $(".modal-content").children().remove();
});